package com.sgb.goods.viewmodel;

import androidx.databinding.ObservableBoolean;
import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;

import com.alibaba.android.arouter.launcher.ARouter;
import com.sgb.goods.api.GoodsCallBack;
import com.sgb.goods.api.GoodsNet;
import com.sgb.goods.entity.IpPickSelectEntity;
import com.sgb.goods.entity.MenuEntity1;
import com.sgb.goods.entity.ProListEntity;
import com.sgb.goods.entity.StorageManEntity;
import com.sgb.goods.entity.VipPriceMangeListEntity;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.utils.StringUtils;
import com.sgb.goods.utils.base.viewmodel.BaseViewModel;
import com.sgb.goods.view.adapter.VipPriceManageListAdapter;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.ListResponse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class VipPriceManageVieModel extends BaseViewModel {

    public VipPriceManageListAdapter adapter;
    public MutableLiveData<List<VipPriceMangeListEntity>> listData = new MutableLiveData<>();
    public MutableLiveData<ProListEntity<List<StorageManEntity>>> storageList = new MutableLiveData<>();
    public MutableLiveData<List<MenuEntity1<MenuEntity1<MenuEntity1>>>> menuEntityList = new MutableLiveData<>();
    public MutableLiveData<String> dialog_show_text = new MutableLiveData<>();
    private int page = 1;
    private int pageStorage = 1;
    public List<IpPickSelectEntity> choiceTypeList = new ArrayList<>();
    public ObservableField<String> itemMemberStatus = new ObservableField<>();//商品状态
    public ObservableField<String> storage = new ObservableField<>();//仓储地址
    public ObservableField<String> storageId = new ObservableField<>();//仓储地址no
    public ObservableField<String> cid1 = new ObservableField<>();//一级分类id
    public ObservableField<String> cid3 = new ObservableField<>();//三级分类id
    public ObservableField<String> goodsSearch = new ObservableField<>();//顶部搜索内容
    /**
     * 是否展开商品分类弹窗
     */
    public ObservableBoolean isOpenGoodsType = new ObservableBoolean();
    /**
     * 是否展开仓储弹窗
     */
    public ObservableBoolean isOpenStorage = new ObservableBoolean();
    /**
     * 是否展开商品状态弹窗
     */
    public ObservableBoolean isOpenGoodsStatus = new ObservableBoolean();


    public VipPriceManageVieModel() {
        choiceTypeList.add(new IpPickSelectEntity("", "全部"));
        choiceTypeList.add(new IpPickSelectEntity("1", "生效中"));
        choiceTypeList.add(new IpPickSelectEntity("0", "已失效"));
        adapter = new VipPriceManageListAdapter();
    }

    public void getData(boolean isLoadMore) {
        page = isLoadMore ? page + 1 : 1;
        HashMap<String, Object> body = new HashMap<>();
        body.put("page", page);
        body.put("limit", Constants.PAGE_SIZE_20);
        body.put("itemMemberStatus", StringUtils.convertEmptyForStr(itemMemberStatus));
        body.put("cid", StringUtils.convertEmptyForStr(cid3));
        body.put("cid1", StringUtils.convertEmptyForStr(cid1));
        body.put("storage", StringUtils.convertEmptyForStr(storageId));
        body.put("title", StringUtils.convertEmptyForStr(goodsSearch));

        showLoading();
        GoodsNet.getInstance().getMemberPriceApi().getMemberPriceGoodsPageList(body).enqueue(new GoodsCallBack<BaseEntity<ListResponse<List<VipPriceMangeListEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<VipPriceMangeListEntity>>> data) {
                if(isLoadMore) {
                    adapter.addData(data.getData().getList());
                } else {
                    adapter.setNewData(data.getData().getList());
                }
                listData.postValue(data.getData().getList());
            }

            @Override
            public void onFail() {
                listData.postValue(null);
            }
        });
    }

    /**
     * 仓储列表
     */
    public void getStorageList(String searchContent, boolean isLodMore) {
        pageStorage = isLodMore ? pageStorage + 1 : 1;
        showLoading();
        GoodsNet.getInstance().getGoodsPublishApi().getStorageList(pageStorage, Constants.PAGE_SIZE_20, searchContent).enqueue(
                new GoodsCallBack<BaseEntity<ProListEntity<List<StorageManEntity>>>>() {
                    @Override
                    public void onSuccess(BaseEntity<ProListEntity<List<StorageManEntity>>> data) {
                        data.getData().isLoadMore = isLodMore;
                        storageList.setValue(data.getData());
                    }

                    @Override
                    public void onFail() {
                        storageList.setValue(null);
                    }
                });
    }

    //获取分类类别
    public void getGoodsCategoryModel() {
        showLoading();
        GoodsNet.getInstance().getGoodsPublishApi().goodsCategory1().enqueue(new GoodsCallBack<BaseEntity<List<MenuEntity1<MenuEntity1<MenuEntity1>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<MenuEntity1<MenuEntity1<MenuEntity1>>>> data) {
                menuEntityList.postValue(data.getData());
            }

            @Override
            public void onFail() {
                menuEntityList.postValue(null);
            }
        });
    }

    //结束当前会员价
    public void finishItemMemberPrice(long itemId, long itemMemberId) {
        showLoading();
        GoodsNet.getInstance().getMemberPriceApi().finishItemMemberPrice(itemId, itemMemberId).enqueue(new GoodsCallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                getData(false);
            }
        });
    }

    /** 再次设置权限验证 */
    public void againItemMemberPrice(long itemId, long itemMemberId) {
        showLoading();
        GoodsNet.getInstance().getMemberPriceApi().againItemMemberPrice(itemId, itemMemberId).enqueue(new GoodsCallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                if ("ok".equals(data.getState())) {
                    ARouter.getInstance().build(Constants.ARouter.AR_ADD_MEMBER_GOODS)
                            .withLong("itemId", itemId).withLong("oldItemMemberId", itemMemberId).navigation();
                } else {
                    dialog_show_text.postValue(data.getMsg());
                }
            }
        });
    }
}
